package controllers

import (
	"encoding/json"
	"fmt"
	"macapp/models"
	"os"
	"strconv"
	"strings"
	"time"
	"unsafe"

	"github.com/astaxie/beego"
)

type AdminSoftListController struct {
	beego.Controller
}

func (a *AdminSoftListController) Index() {
	a.TplName = "admin/softlist.html"
}

func (a *AdminSoftListController) GetAll() {
	var softjson models.SoftJson
	var soft []models.Soft
	var tmp_cf models.Classification

	page := a.GetString("page")
	limit := a.GetString("limit")
	int_page, _ := strconv.Atoi(page)
	int_limit, _ := strconv.Atoi(limit)

	qs := models.O.QueryTable(new(models.Soft))
	qs_cf := models.O.QueryTable(new(models.Classification))
	qs.Limit(int_limit, (int_page-1)*int_limit).OrderBy("-id").All(&soft)

	for k, v := range soft {
		qs_cf.Filter("Id", v.Softcf).One(&tmp_cf)
		soft[k].Softcf = tmp_cf.Cfname
	}

	softjson.Code = 0
	softjson.Msg = ""
	softjson.Count = GetSoftCounts()
	softjson.Data = soft
	a.Data["json"] = &softjson
	a.ServeJSON()
}

func (a *AdminSoftListController) Add() {
	var soft_cf []models.Classification

	a.Data["id"] = strings.ToUpper(models.GetRandomString(16))

	//查询分类
	qs := models.O.QueryTable("classification")
	qs.All(&soft_cf)
	a.Data["cfcation"] = soft_cf
	a.TplName = "admin/soft-add.html"
}

func (a *AdminSoftListController) Edit() {
	var soft models.Soft
	var cf_one models.Classification
	var soft_cf []models.Classification
	soft.Id = a.Ctx.Input.Param(":softid")
	models.O.Read(&soft)

	cf_one.Id = soft.Softcf
	models.O.Read(&cf_one)

	//查询所有分类
	qs := models.O.QueryTable("classification")
	qs.All(&soft_cf)
	a.Data["cfcation"] = soft_cf

	a.Data["id"] = soft.Id
	a.Data["softname"] = soft.Softname
	a.Data["version"] = soft.Version
	a.Data["softcf"] = soft.Softcf
	a.Data["cfname"] = cf_one.Cfname
	a.Data["downloadaddr"] = soft.Downloadaddr
	a.Data["downloadpwd"] = soft.Downloadpwd
	a.Data["softico"] = soft.Softico
	a.Data["title"] = soft.Title
	a.Data["keyword"] = soft.Keyword
	a.Data["body"] = soft.Body

	a.TplName = "admin/soft-edit.html"
}

func (a *AdminSoftListController) AddApi() {
	var soft models.Soft
	soft.Id = a.GetString("formtext[id]")
	soft.Softname = a.GetString("formtext[softname]")
	soft.Version = a.GetString("formtext[softver]")
	soft.Softcf = a.GetString("formtext[cfcation]")
	soft.Downloadaddr = a.GetString("formtext[softaddr]")
	soft.Downloadpwd = a.GetString("formtext[softpwd]")
	soft.Title = a.GetString("formtext[softtitle]")
	soft.Keyword = a.GetString("formtext[softkeyword]")
	soft.Softico = a.GetString("imgsrc")
	soft.Body = a.GetString("edittext")
	soft.Updatetime = time.Now().Format("2006/01/02 15:04:05")

	_, err := models.O.Insert(&soft)
	fmt.Println(err)
	a.Ctx.WriteString("success ")
}

func (a *AdminSoftListController) ImgUpLoad() {
	var upload_rep models.UploadRep
	f, h, _ := a.GetFile("file")
	ys_name := h.Filename
	index_file := strings.Index(h.Filename, ".")
	h.Filename = a.GetString("id") + ys_name[index_file:len(ys_name)]
	//先删除目录下已经有的图片，在写入
	os_remove_err := os.Remove("static/img/" + h.Filename)
	if os_remove_err != nil {
		fmt.Println("%s", os_remove_err)
	} else {
		fmt.Println(h.Filename + ">>>删除成功")
	}
	a.SaveToFile("file", "static/img/"+h.Filename)

	upload_rep.Code = 0
	upload_rep.Msg = ""
	upload_rep.Data.Src = "http://127.0.0.1:8080/static/img/" + h.Filename

	upload_rep_json, _ := json.Marshal(upload_rep)
	defer f.Close()
	a.Ctx.WriteString(string(upload_rep_json))
}

func (a *AdminSoftListController) EditImgUpLoad() {
	var upload_rep models.UploadRep
	f, h, _ := a.GetFile("file")
	ys_name := h.Filename
	index_file := strings.Index(h.Filename, ".")
	h.Filename = models.GetRandomString(16) + ys_name[index_file:len(ys_name)]
	//先删除目录下已经有的图片，在写入
	os_remove_err := os.Remove("static/img/" + h.Filename)
	if os_remove_err != nil {
		fmt.Println("%s", os_remove_err)
	} else {
		fmt.Println(h.Filename + ">>>删除成功")
	}
	a.SaveToFile("file", "static/img/"+h.Filename)

	upload_rep.Code = 0
	upload_rep.Msg = ""
	upload_rep.Data.Src = "http://127.0.0.1:8080/static/img/" + h.Filename

	upload_rep_json, _ := json.Marshal(upload_rep)
	defer f.Close()
	a.Ctx.WriteString(string(upload_rep_json))
}

func GetSoftCounts() int {
	int64_cnt, _ := models.O.QueryTable(new(models.Soft)).Count()
	//int64 转int
	cnt := *(*int)(unsafe.Pointer(&int64_cnt))
	return cnt
}
